<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="110px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="站点名称" prop="siteName">
						<el-select v-model="form.siteName" placeholder="请选择站点">
							<el-option :key="index" :label="item.label" :value="item.value" v-for="(item, index) in siteNameOptions"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="开始时间" prop="startTime">
						<el-time-picker placeholder="请选择开始时间" v-model="form.startTime" format="HH:mm:ss" value-format="HH:mm:ss" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="结束时间" prop="endTime">
						<el-time-picker placeholder="请选择结束时间" v-model="form.endTime" format="HH:mm:ss" value-format="HH:mm:ss" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="误差范围(米)" prop="errorRange">
						<el-input-number :min="1" :max="1000" v-model="form.errorRange" placeholder="请输入误差范围"></el-input-number>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="AirGridMemberTrajectorySettingDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/platform/airGridMemberTrajectorySetting';
import { rule } from '/@/utils/validate';
import { fetchList } from '/@/api/platform/airSite';
import { useRoute } from 'vue-router';

const emit = defineEmits(['refresh']);
const route = useRoute();
// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const siteNameOptions = ref([]);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	gridMemberId: route.query.id,
	siteName: '',
	startTime: '',
	endTime: '',
	errorRange: 0,
});

// 定义校验规则
const dataRules = ref({
	siteName: [{ required: true, message: '站点名称不能为空', trigger: 'blur' }],
	startTime: [
		{ required: true, message: '开始时间不能为空', trigger: 'blur' },
		{
			validator: (rule: any, value: any, callback: any) => {
				console.log(value);
				if (form.endTime && value >= form.endTime) {
					callback(new Error('开始时间必须小于结束时间'));
				} else {
					callback();
				}
			},
			message: '开始时间必须小于结束时间',
			trigger: 'change',
		},
	],
	endTime: [
		{ required: true, message: '结束时间不能为空', trigger: 'blur' },
		{
			validator: (rule: any, value: any, callback: any) => {
				if (form.startTime && value <= form.startTime) {
					callback(new Error('结束时间必须大于开始时间'));
				} else {
					callback();
				}
			},
			message: '结束时间必须大于开始时间',
			trigger: 'change',
		},
	],
	errorRange: [{ required: true, message: '误差范围不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取airGridMemberTrajectorySetting信息
	if (id) {
		form.id = id;
		getairGridMemberTrajectorySettingData(id);
	} else {
		getSiteNameOptions();
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getairGridMemberTrajectorySettingData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
			getSiteNameOptions();
		})
		.finally(() => {
			loading.value = false;
		});
};
// 获取站点信息
const getSiteNameOptions = () => {
	fetchList({ tag: '1111111111111111111' }).then((res) => {
		siteNameOptions.value = res.data.records.map((d) => {
			return {
				label: d.name,
				value: d.name,
			};
		});
	});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
